package org.carshared.principal;

import java.util.ArrayList;

import org.carshared.R;
import org.carshared.sqlite.DBAdapterDateTravel;
import org.carshared.sqlite.DBAdapterPassenger;
import org.carshared.sqlite.DBAdapterTravel;
import org.carshared.utils.PassengerBeen;
import org.carshared.utils.TravelBeen;

import android.app.Activity;
import android.content.Intent;
import android.os.Bundle;
import android.view.View;
import android.widget.CheckBox;
import android.widget.TextView;

import com.google.gson.Gson;

public class EditTravelActivity extends Activity {

	private Gson gson;
	private String travel;
	private TravelBeen dataTravel;
	private DBAdapterPassenger dbHelperPassenger;
	private DBAdapterTravel dbHelper;
	private DBAdapterDateTravel dbHelperDateTravel;
	private ArrayList<PassengerBeen> p;
	private TextView p1;
	private TextView p2;
	private TextView p3;
	private TextView p4;
	private CheckBox c1;
	private CheckBox c2;
	private CheckBox c3;
	private CheckBox c4;

	@Override
	protected void onCreate(Bundle savedInstanceState) {
		// TODO Auto-generated method stub
		super.onCreate(savedInstanceState);
		setContentView(R.layout.activity_edit_travel);

		Bundle bundle = getIntent().getExtras();
		gson = new Gson();
		travel = bundle.getString("dataTravel");
		dataTravel = gson.fromJson(travel, TravelBeen.class);
		dbHelperPassenger = new DBAdapterPassenger(this);
		dbHelper = new DBAdapterTravel(this);
		dbHelperDateTravel = new DBAdapterDateTravel(this);
		p = new ArrayList<PassengerBeen>();

		if (("owner").equals(dataTravel.getTipo())) {
			p = dbHelperPassenger.fetchAllPassenger(dataTravel
					.getOrigenDestino());
		} else {
			p.add(dbHelperPassenger.fetchPassenger(dataTravel
					.getOrigenDestino()));
		}

		p1 = (TextView) findViewById(R.id.textView1);
		p2 = (TextView) findViewById(R.id.namePassenger1);
		p3 = (TextView) findViewById(R.id.textView3);
		p4 = (TextView) findViewById(R.id.namePassenger2);
		c1 = (CheckBox) findViewById(R.id.checkBox1);
		c2 = (CheckBox) findViewById(R.id.CheckBox01);
		c3 = (CheckBox) findViewById(R.id.CheckBox02);
		c4 = (CheckBox) findViewById(R.id.CheckBox03);

		setDatasPassenger();

	}

	private void setDatasPassenger() {
		// TODO Auto-generated method stub
		switch (dataTravel.getnPasajeros()) {
		case 0:
		case 1:
			p1.setText(p.get(0).getNombrePasajero());
			c1.setChecked(p.get(0).isPagado());
			break;
		case 2:
			p2.setEnabled(true);
			c2.setEnabled(true);

			p1.setText(p.get(0).getNombrePasajero());
			p2.setText(p.get(1).getNombrePasajero());
			c1.setChecked(p.get(0).isPagado());
			c2.setChecked(p.get(1).isPagado());
			break;
		case 3:
			p2.setEnabled(true);
			c2.setEnabled(true);
			p3.setEnabled(true);
			c3.setEnabled(true);

			p1.setText(p.get(0).getNombrePasajero());
			p2.setText(p.get(1).getNombrePasajero());
			p3.setText(p.get(2).getNombrePasajero());
			c1.setChecked(p.get(0).isPagado());
			c2.setChecked(p.get(1).isPagado());
			c3.setChecked(p.get(2).isPagado());
			break;
		default:
			p2.setEnabled(true);
			c2.setEnabled(true);
			p3.setEnabled(true);
			c3.setEnabled(true);
			p4.setEnabled(true);
			c4.setEnabled(true);

			p1.setText(p.get(0).getNombrePasajero());
			p2.setText(p.get(1).getNombrePasajero());
			p3.setText(p.get(2).getNombrePasajero());
			p4.setText(p.get(3).getNombrePasajero());
			c1.setChecked(p.get(0).isPagado());
			c2.setChecked(p.get(1).isPagado());
			c3.setChecked(p.get(2).isPagado());
			c4.setChecked(p.get(3).isPagado());
			break;
		}

	}

	public void saveData(View view) {

		switch (dataTravel.getnPasajeros()) {
		case 0:
		case 1:

			p.get(0).setPagado(c1.isChecked());
			if (p.get(0).isPagado()) {
				dataTravel.setAcumulado(dataTravel.getAcumulado()
						- p.get(0).getAcumulado());
				p.get(0).setAcumulado(0);
			}
			break;
		case 2:

			p.get(0).setPagado(c1.isChecked());
			p.get(1).setPagado(c2.isChecked());

			if (p.get(0).isPagado()) {
				dataTravel.setAcumulado(dataTravel.getAcumulado()
						- p.get(0).getAcumulado());
				p.get(0).setAcumulado(0);
			}
			if (p.get(1).isPagado()) {
				dataTravel.setAcumulado(dataTravel.getAcumulado()
						- p.get(1).getAcumulado());
				p.get(1).setAcumulado(0);
			}
			break;
		case 3:

			p.get(0).setPagado(c1.isChecked());
			p.get(1).setPagado(c2.isChecked());
			p.get(2).setPagado(c3.isChecked());

			if (p.get(0).isPagado()) {

				dataTravel.setAcumulado(dataTravel.getAcumulado()
						- p.get(0).getAcumulado());
				p.get(0).setAcumulado(0);
			}
			if (p.get(1).isPagado()) {
				dataTravel.setAcumulado(dataTravel.getAcumulado()
						- p.get(1).getAcumulado());
				p.get(1).setAcumulado(0);
			}
			if (p.get(2).isPagado()) {
				dataTravel.setAcumulado(dataTravel.getAcumulado()
						- p.get(2).getAcumulado());
				p.get(2).setAcumulado(0);
			}
			break;
		default:
			p.get(0).setPagado(c1.isChecked());
			p.get(1).setPagado(c2.isChecked());
			p.get(2).setPagado(c3.isChecked());
			p.get(3).setPagado(c4.isChecked());
			if (p.get(0).isPagado()) {
				dataTravel.setAcumulado(dataTravel.getAcumulado()
						- p.get(0).getAcumulado());
				p.get(0).setAcumulado(0);
			}
			if (p.get(1).isPagado()) {
				dataTravel.setAcumulado(dataTravel.getAcumulado()
						- p.get(1).getAcumulado());
				p.get(1).setAcumulado(0);
			}
			if (p.get(2).isPagado()) {
				dataTravel.setAcumulado(dataTravel.getAcumulado()
						- p.get(2).getAcumulado());
				p.get(2).setAcumulado(0);
			}
			if (p.get(3).isPagado()) {
				dataTravel.setAcumulado(dataTravel.getAcumulado()
						- p.get(3).getAcumulado());
				p.get(3).setAcumulado(0);
			}

			break;
		}

		for (PassengerBeen passenger : p) {
			dbHelperPassenger.updatePassenger(passenger);
			if (passenger.isPagado()) {
				dbHelperDateTravel.deleteDataPassenger(passenger);
			}

		}
		dbHelper.update(dataTravel);

		Intent returnIntent = new Intent();
		setResult(RESULT_OK, returnIntent);
		EditTravelActivity.this.finish();

	}

	public void cancelData(View view) {

		EditTravelActivity.this.finish();
	}
}
